home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / RCS / setenv.c,v < prev    next >
Encoding:
Text File  |  1991-06-25  |  5.4 KB  |  265 lines

  1. head    5.2;
  2. branch    5.2.0;
  3. access;
  4. symbols
  5.     RELEASE:5.2.0.4
  6.     BETA:5.2.0.3
  7.     UICSO:5.2.0
  8.     VANILLA:5.2;
  9. locks; strict;
  10. comment    @ * @;
  11.  
  12.  
  13. 5.2
  14. date    91.05.17.18.43.10;    author paul;    state Exp;
  15. branches
  16.     5.2.0.1;
  17. next    ;
  18.  
  19. 5.2.0.1
  20. date    91.05.18.18.21.48;    author paul;    state Exp;
  21. branches;
  22. next    5.2.0.2;
  23.  
  24. 5.2.0.2
  25. date    91.05.18.23.04.30;    author paul;    state Exp;
  26. branches;
  27. next    5.2.0.3;
  28.  
  29. 5.2.0.3
  30. date    91.05.23.21.42.15;    author paul;    state Exp;
  31. branches;
  32. next    5.2.0.4;
  33.  
  34. 5.2.0.4
  35. date    91.06.24.20.19.37;    author paul;    state Exp;
  36. branches;
  37. next    ;
  38.  
  39.  
  40. desc
  41. @Set/Unset variables in the environment.
  42. @
  43.  
  44.  
  45. 5.2
  46. log
  47. @5.65 distribution
  48. @
  49. text
  50. @/*
  51.  * Copyright (c) 1987 Regents of the University of California.
  52.  * All rights reserved.
  53.  *
  54.  * Redistribution and use in source and binary forms are permitted
  55.  * provided that the above copyright notice and this paragraph are
  56.  * duplicated in all such forms and that any documentation,
  57.  * advertising materials, and other materials related to such
  58.  * distribution and use acknowledge that the software was developed
  59.  * by the University of California, Berkeley.  The name of the
  60.  * University may not be used to endorse or promote products derived
  61.  * from this software without specific prior written permission.
  62.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  63.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  64.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  65.  */
  66.  
  67. #if defined(LIBC_SCCS) && !defined(lint)
  68. static char sccsid[] = "@@(#)setenv.c    5.2 (Berkeley) 6/27/88";
  69. #endif /* LIBC_SCCS and not lint */
  70.  
  71. char *malloc(), *realloc(), *_findenv();
  72.  
  73. /*
  74.  * setenv --
  75.  *    Set the value of the environmental variable "name" to be
  76.  *    "value".  If rewrite is set, replace any current value.
  77.  */
  78. setenv(name, value, rewrite)
  79.     register char *name, *value;
  80.     int rewrite;
  81. {
  82.     extern char **environ;
  83.     static int alloced;            /* if allocated space before */
  84.     register char *C;
  85.     int l_value, offset;
  86.  
  87.     if (*value == '=')            /* no `=' in value */
  88.         ++value;
  89.     l_value = strlen(value);
  90.     if ((C = _findenv(name, &offset))) {    /* find if already exists */
  91.         if (!rewrite)
  92.             return(0);
  93.         if (strlen(C) >= l_value) {    /* old larger; copy over */
  94.             while (*C++ = *value++);
  95.             return(0);
  96.         }
  97.     }
  98.     else {                    /* create new slot */
  99.         register int    cnt;
  100.         register char    **P;
  101.  
  102.         for (P = environ, cnt = 0; *P; ++P, ++cnt);
  103.         if (alloced) {            /* just increase size */
  104.             environ = (char **)realloc((char *)environ,
  105.                 (u_int)(sizeof(char *) * (cnt + 2)));
  106.             if (!environ)
  107.                 return(-1);
  108.         }
  109.         else {                /* get new space */
  110.             alloced = 1;        /* copy old entries into it */
  111.             P = (char **)malloc((u_int)(sizeof(char *) *
  112.                 (cnt + 2)));
  113.             if (!P)
  114.                 return(-1);
  115.             bcopy(environ, P, cnt * sizeof(char *));
  116.             environ = P;
  117.         }
  118.         environ[cnt + 1] = NULL;
  119.         offset = cnt;
  120.     }
  121.     for (C = name; *C && *C != '='; ++C);    /* no `=' in name */
  122.     if (!(environ[offset] =            /* name + `=' + value */
  123.         malloc((u_int)((int)(C - name) + l_value + 2))))
  124.         return(-1);
  125.     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
  126.     for (*C++ = '='; *C++ = *value++;);
  127.     return(0);
  128. }
  129.  
  130. /*
  131.  * unsetenv(name) --
  132.  *    Delete environmental variable "name".
  133.  */
  134. void
  135. unsetenv(name)
  136.     char    *name;
  137. {
  138.     extern    char    **environ;
  139.     register char    **P;
  140.     int    offset;
  141.  
  142.     while (_findenv(name, &offset))        /* if set multiple times */
  143.         for (P = &environ[offset];; ++P)
  144.             if (!(*P = *(P + 1)))
  145.                 break;
  146. }
  147. @
  148.  
  149.  
  150. 5.2.0.1
  151. log
  152. @Added contents of getenv.c appropriately #ifdef'ed
  153. @
  154. text
  155. @d18 3
  156. a20 4
  157. #if !defined(lint)
  158. static char sccsid[] = "@@(#)setenv.c    5.2 (Berkeley) 6/27/88,    %I% local";
  159. static char rcsid[] = "@@(#)$Id$";
  160. #endif /* not lint */
  161. d22 1
  162. a22 1
  163. #include "sendmail.h"
  164. a23 1
  165. #if defined(NEED_GETENV) || defined(NEED_SETENV) || defined(NEED_UNSETENV)
  166. a24 28
  167.  * _findenv --
  168.  *    Returns pointer to value associated with name, if any, else NULL.
  169.  *    Sets offset to be the offset of the name/value combination in the
  170.  *    environmental array, for use by setenv(3) and unsetenv(3).
  171.  *    Explicitly removes '=' in argument name.
  172.  */
  173. static char *
  174. _findenv(name, offset)
  175.     register char *name;
  176.     int *offset;
  177. {
  178.     extern char **environ;
  179.     register int len;
  180.     register char **P, *C;
  181.  
  182.     for (C = name, len = 0; *C && *C != '='; ++C, ++len);
  183.     for (P = environ; *P; ++P)
  184.         if (!strncmp(*P, name, len))
  185.             if (*(C = *P + len) == '=') {
  186.                 *offset = P - environ;
  187.                 return(++C);
  188.             }
  189.     return(NULL);
  190. }
  191. #endif /* NEED_GETENV || NEED_SETENV || NEED_UNSETENV */
  192.  
  193. #ifdef NEED_SETENV
  194. /*
  195. a79 1
  196. #endif /* NEED_SETENV */
  197. a80 1
  198. #ifdef NEED_UNSETENV
  199. a97 16
  200. #endif /* NEED_UNSETENV */
  201.  
  202. #ifdef NEED_GETENV
  203. /*
  204.  * getenv --
  205.  *    Returns ptr to value associated with name, if any, else NULL.
  206.  */
  207. char *
  208. getenv(name)
  209.     char *name;
  210. {
  211.     int offset;
  212.  
  213.     return(_findenv(name, &offset));
  214. }
  215. #endif /* NEED_GETENV */
  216. @
  217.  
  218.  
  219. 5.2.0.2
  220. log
  221. @Re-ordered #include "sendmail.h" .
  222. @
  223. text
  224. @a17 2
  225. #include "sendmail.h"
  226.  
  227. d20 1
  228. a20 1
  229. static char rcsid[] = "@@(#)$Id: setenv.c,v 5.2.0.1 1991/05/18 18:21:48 paul Exp paul $";
  230. d22 2
  231. @
  232.  
  233.  
  234. 5.2.0.3
  235. log
  236. @Further changes for Interactive Systems UNIX adapted from patches
  237. sent by Andy Linton <Andy.Linton@@comp.vuw.ac.nz>.
  238. @
  239. text
  240. @d22 1
  241. a22 1
  242. static char rcsid[] = "@@(#)$Id: setenv.c,v 5.2.0.2 1991/05/18 23:04:30 paul Exp paul $";
  243. a23 4
  244.  
  245. #if defined(ISC)
  246. # include <sys/bsdtypes.h>
  247. #endif /* ISC */
  248. @
  249.  
  250.  
  251. 5.2.0.4
  252. log
  253. @Relocated sccsid/rcsid inside NEED_* wrapper.
  254. @
  255. text
  256. @d20 5
  257. a29 6
  258.  
  259. # if !defined(lint)
  260. static char sccsid[] = "@@(#)setenv.c    5.2 (Berkeley) 6/27/88";
  261. static char rcsid[] = "@@(#)$Id: setenv.c,v 5.2.0.3 1991/05/23 21:42:15 paul Exp $";
  262. # endif /* not lint */
  263.  
  264. @
  265.